//==========================================================================
// Copyright (c) 2000-2006,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include "display_def.h"
#include <util.h>

#define DMA_FAST_PAGE1      0xa0050000
#define BASE_PLANE_BASE     0xa0000000

static void ClearBaseplane(DisplayContext *pCxt)
{
	memset((void *)pCxt->vaBasePlane, 0x0, pCxt->uChan0Size);
}

static BOOL SetBaseplaneBuffer(DisplayContext *pCxt)
{
	// Alloc Buffer for baseplane framebuffer
	pCxt->paBasePlane = BASE_PLANE_BASE;

	return TRUE;
}
static void SetDisplayDesc(DisplayContext *pCxt)
{
	// Alloc Buffer for all channel DMA descriptors, 1 page OK.
	pCxt->vaChan0Desc = DMA_FAST_PAGE1;
	pCxt->paChan0Desc = DMA_FAST_PAGE1;

	pCxt->vaChan1Desc = pCxt->vaChan0Desc  + DESC_SIZE;
	pCxt->vaChan2Desc = pCxt->vaChan1Desc  + DESC_SIZE;
	pCxt->vaChan3Desc = pCxt->vaChan2Desc  + DESC_SIZE;
	pCxt->vaChan4Desc = pCxt->vaChan3Desc  + DESC_SIZE;
	pCxt->vaChan1AltDesc = pCxt->vaChan4Desc + DESC_SIZE;
	pCxt->vaPaletteDesc = pCxt->vaChan1AltDesc + DESC_SIZE;

	pCxt->paChan1Desc = pCxt->paChan0Desc  + DESC_SIZE;
	pCxt->paChan2Desc = pCxt->paChan1Desc  + DESC_SIZE;
	pCxt->paChan3Desc = pCxt->paChan2Desc  + DESC_SIZE;
	pCxt->paChan4Desc = pCxt->paChan3Desc  + DESC_SIZE;
	pCxt->paChan1AltDesc = pCxt->paChan4Desc + DESC_SIZE;
	pCxt->paPaletteDesc = pCxt->paChan1AltDesc + DESC_SIZE;
}

static void LcdParamInit(DisplayContext *pCxt)
{
	pCxt->basePlaneFormat = BASEPLANE_COLORFORMAT;
	pCxt->vaBasePlane = pCxt->paBasePlane;
	pCxt->overlay[1].Enable = FALSE;
	pCxt->overlay[2].Enable = FALSE;
}

ECODE Display_Create(DisplayContext *pCxt)
{
	SetDisplayDesc(pCxt);
	SetBaseplaneBuffer(pCxt);
	LcdParamInit(pCxt);
	return NOERROR;
}

ECODE Display_Init(DisplayContext *pCxt)
{
	pCxt->uChan0Size = BASEPLANE_FB_SIZE;
	// Before init, disable LCD controller
	LcdCtrl_Disable(pCxt);
	// Open back light
	//    BackLight_Open(pCxt);
	Lcm_DisplayOn(pCxt);
	// Initialize the LCD Controller and frame descriptors
	LcdCtrl_Init(pCxt);
	// Enable the LCD controller
	LcdCtrl_Enable(pCxt);

	BackLight_Open();

	ClearBaseplane(pCxt);

	return NOERROR;
}

static DisplayContext m_disCxt;
void Xs_LCD_Init()
{
	Display_Create(&m_disCxt);
	Display_Init(&m_disCxt);

	//    printf("Xs_LCD_INIT OK \n");
}

int Do_InitLcd(void)
{
    static BOOL bInitLcd = FALSE;
    printf("bInitLcd %d\n",bInitLcd);
    if (!bInitLcd) {
        Xs_LCD_Init();
        bInitLcd = TRUE;
    }
    printk("\nm_disCxt.paBasePlane= %x\n", m_disCxt.paBasePlane);
    return 0;
}

void write_data_disCxt(int offset, int size, unsigned short data)
{
    unsigned short *pFrame= (unsigned short *)m_disCxt.paBasePlane;
    if (NULL == pFrame) return ;
    pFrame += offset;

    int i;
    for (i=0; i < size; i++) {
        pFrame[i] = data;
    }
    return ;
}

unsigned int GetBasePlaneBufAddr(void)
{
	return m_disCxt.paBasePlane;
}

//int ReadLogo(int ptr, int size);
void msleep(unsigned int ms);
extern unsigned char logo[];
extern int bNormalBoot;

void TestColorDisplay()
{
	// 16 bits .test color.
	DisplayContext *pCxt = &m_disCxt;
	unsigned short nColorType;
	unsigned short nColorBlue[]  = { 0x0000, 0x0010, 0x001F };
	unsigned short nColorGreen[] = { 0x0000, 0x0400, 0x07E0 };
	unsigned short nColorRed[]   = { 0x0000, 0x8000, 0xF800 };
	int m, n;
	int i, j, k;
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 3; j++) {
			for (k = 0; k < 3; k++) {
				nColorType = nColorRed[k] | nColorBlue[i] | nColorGreen[j];
				for (m = 0; m < LCD_HEIGHT; m++) {
					for (n = 0; n < LCD_WIDTH; n++) {
						((unsigned short *)pCxt->vaBasePlane)[m * LCD_WIDTH + n] = nColorType;
					}
				}
				ostdelay(1000000);
			}
		}
	}
	return ;
}

#if 1 //Initial ZZB
void Display_Logo()
{
	//  ReadLogo((int)m_disCxt.paBasePlane,240*320*2);
	//    memcpy((void *)m_disCxt.paBasePlane, logo, 240*320*2);
	if(bNormalBoot) {
		//msleep(150);//Wait for lcm init ok
		// backLight_Open();
	}
}
#else
void backlight_open();
void Display_Logo()
{
	 ReadLogo((int)m_disCxt.paBasePlane,240*320*2);
	    memcpy((void *)m_disCxt.paBasePlane, logo, 240*320*2);
        if(bNormalBoot) {
            msleep(150);//Wait for lcm init ok
            backLight_Open();
        }
}
#endif

void backlight_on();
void backlight_open()
{
	// BackLight_Open();
    backlight_on();
}
